package com.lamatek.tags.google.beans;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.StringTokenizer;

/**
 * IntlAddressGeocoder
 * @author Tom Cole
 * @version 0.40
 * 
 * This class takes an international address and attempts to geocode it to
 * longitude/latitude using Maporama.com.
 * 
 * Developers should not override this class, but rather should override
 * the Geocoder.geocode() method to call the geocoder of their choice.
 */
public class IntlAddressGeocoder implements Geocoder, Serializable {
    
    String zipcode = "";
    String state = "";
    String city = null;
    String address = "";
    String country = null;
    double longitude = 0.00d;
    double latitude = 0.00d;

    public void setAddress(String address) {
        this.address = address;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public void setState(String state) {
        this.state = state;
    }
    public void setCountry(String country) {
       this.country = country;
    }
    public void setZip(String zip) {
        this.zipcode = zip;
    }
    public boolean geocode() {
        if (country == null || city == null) {
            return false;
        }
        else try {
            String path = "http://www.maporama.com/share/Map.asp?";
            path += "COUNTRYCODE=" + country;
            path += "&_XgoGCAddress=" + URLEncoder.encode(address, "UTF-8");
            path += "&_XgoGCTownName=" + URLEncoder.encode(city.trim(), "UTF-8");
            path += "&State=" + URLEncoder.encode(state, "UTF-8");
            path += "&Zip=" + URLEncoder.encode(zipcode, "UTF-8");
            URL url = new URL(path);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            BufferedReader input = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String line = "";
            int count = 0;
            while ((line = input.readLine()) != null) {
                if (line.indexOf("Lat-Long") > 0) {
                    int start = line.indexOf("SearchMapFontText", line.indexOf("Lat-Long"));
                    start = line.indexOf("SearchMapFontText", start + 1);
                    start = line.indexOf(">", start) + 1;
                    int end = line.indexOf("<", start);
                    String coords = line.substring(start, end);
                    StringTokenizer t = new StringTokenizer(coords, ",");
                    try {
                        latitude = Double.parseDouble(t.nextToken().trim());
                        longitude = Double.parseDouble(t.nextToken().trim());
                    }
                    catch(Exception e) {
                        e.printStackTrace(System.err);
                        return false;
                    }
                }
            }
            input.close();
            return true;
        }
        catch(Exception ex) {
            ex.printStackTrace(System.err);
            return false;
        }
    }
    public double getLongitude() {
        return longitude;
    }
    public double getLatitude() {
        return latitude;
    }
    public String getPrecision() {
        return null;
    }
    public String getWarning() {
        return null;
    }
}
